$OpenBSD: patch-src_ui_tools_flood-tool_cpp,v 1.1 2017/05/22 15:02:40 espie Exp $
int - unsigned yields unsigned. More sturdy absdiff pattern

Index: src/ui/tools/flood-tool.cpp
--- src/ui/tools/flood-tool.cpp.orig
+++ src/ui/tools/flood-tool.cpp
@@ -195,6 +195,12 @@ inline unsigned char * get_trace_pixel(guchar *trace_p
     return trace_px + (x + y * width);
 }
 
+template<class T>inline T absdiff(T a, T b)
+{
+    return a < b ? b - a : a - b;
+}
+
+
 /**
  * Compare a pixel in a pixel buffer with another pixel to determine if a point should be included in the fill operation.
  * @param check The pixel in the pixel buffer to check.
@@ -232,13 +238,13 @@ static bool compare_pixels(guint32 check, guint32 orig
     
     switch (method) {
         case FLOOD_CHANNELS_ALPHA:
-            return abs(static_cast<int>(ac) - ao) <= threshold;
+            return absdiff(ac, ao) <= threshold;
         case FLOOD_CHANNELS_R:
-            return abs(static_cast<int>(ac ? unpremul_alpha(rc, ac) : 0) - (ao ? unpremul_alpha(ro, ao) : 0)) <= threshold;
+            return absdiff(ac ? unpremul_alpha(rc, ac) : 0, ao ? unpremul_alpha(ro, ao) : 0) <= threshold;
         case FLOOD_CHANNELS_G:
-            return abs(static_cast<int>(ac ? unpremul_alpha(gc, ac) : 0) - (ao ? unpremul_alpha(go, ao) : 0)) <= threshold;
+            return absdiff(ac ? unpremul_alpha(gc, ac) : 0, ao ? unpremul_alpha(go, ao) : 0) <= threshold;
         case FLOOD_CHANNELS_B:
-            return abs(static_cast<int>(ac ? unpremul_alpha(bc, ac) : 0) - (ao ? unpremul_alpha(bo, ao) : 0)) <= threshold;
+            return absdiff(ac ? unpremul_alpha(bc, ac) : 0, ao ? unpremul_alpha(bo, ao) : 0) <= threshold;
         case FLOOD_CHANNELS_RGB:
             guint32 amc, rmc, bmc, gmc;
             //amc = 255*255 - (255-ac)*(255-ad); amc = (amc + 127) / 255;
@@ -248,9 +254,9 @@ static bool compare_pixels(guint32 check, guint32 orig
             gmc = (255-ac)*gd + 255*gc; gmc = (gmc + 127) / 255;
             bmc = (255-ac)*bd + 255*bc; bmc = (bmc + 127) / 255;
 
-            diff += abs(static_cast<int>(amc ? unpremul_alpha(rmc, amc) : 0) - (amop ? unpremul_alpha(rmop, amop) : 0));
-            diff += abs(static_cast<int>(amc ? unpremul_alpha(gmc, amc) : 0) - (amop ? unpremul_alpha(gmop, amop) : 0));
-            diff += abs(static_cast<int>(amc ? unpremul_alpha(bmc, amc) : 0) - (amop ? unpremul_alpha(bmop, amop) : 0));
+            diff += absdiff(amc ? unpremul_alpha(rmc, amc) : 0,  amop ? unpremul_alpha(rmop, amop) : 0);
+            diff += absdiff(amc ? unpremul_alpha(gmc, amc) : 0, amop ? unpremul_alpha(gmop, amop) : 0);
+            diff += absdiff(amc ? unpremul_alpha(bmc, amc) : 0, amop ? unpremul_alpha(bmop, amop) : 0);
             return ((diff / 3) <= ((threshold * 3) / 4));
         
         case FLOOD_CHANNELS_H:
